home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / GUI.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  12.4 KB  |  394 lines

  1. #define __USE_SYSBASE
  2. #include <proto/exec.h>
  3. #include <proto/intuition.h>
  4. #include <proto/commodities.h>
  5. #include <proto/gadtools.h>
  6. #include <proto/graphics.h>
  7. #include <proto/utility.h>
  8. #include <proto/diskfont.h>
  9. #include <intuition/intuitionbase.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <extras/gui.h>
  15. #include <extras/ext_text.h>
  16. #include <clib/extras_protos.h>
  17.  
  18. /****** extras.lib/OBSOLETE_MakeGadgets ******************************************
  19. *
  20. *   OBSOLETE
  21. *       Since OS3.5 uses Reaction/ClassAct - this code is obsolete
  22. *
  23. *   NAME
  24. *       MakeGadgets -- Minimal layout Gadtools gadgets.
  25. *
  26. *   SYNOPSIS
  27. *       Gadget = MakeGedgets(Scr, VisualInfo, NumGadgets, NewGads, 
  28. *               NewGadTags, NewGadKinds, Gadgets, TextAttr, XScale,
  29. *               YScale)
  30. *
  31. *       struct Gadget * MakeGadgets(struct Screen *,APTR ,ULONG,
  32. *               struct NewGadgets *, ULONG *, ULONG *,
  33. *               struct Gadget **, struct TextAttr *, float, float);
  34. *
  35. *   FUNCTION
  36. *       This function will create Gadgets from an array of
  37. *       Gadtools NewGadgets, scaling the gadgets to the size
  38. *       specified.
  39. *
  40. *   INPUTS
  41. *       Scr - This is the screen that the window containing these
  42. *                gadgets are destine for.  This is only used to get
  43. *                the WBorX values used to offset the gadgets from
  44. *                the window border.  *Set this to NULL if the gadgets
  45. *                are destine for a GimmeZeroZero window*.
  46. *       VisualInfo - VisualInfo pointer from gadtools/GetVisualInfo().
  47. *       NumGadgets - the number of gadgets to process.
  48. *       NewGads - array of struct NewGadget.
  49. *       NewGadTags - array of ULONGS to be processed as TagItems.
  50. *             All the gadget's tags are in this single array.
  51. *             (In the same fashion as GadToolsBox) 
  52. *             Individual tag arrays are NULL(TAG_DONE) terminated,
  53. *             And you may use TAG_MORE for additional Tags per gadget,
  54. *             but the array still must be NULL terminated.
  55. *       NewGadKinds - array of ULONGs, (BUTTON_KIND etc.)
  56. *       Gadgets - (struct Gadget **) Pointer to an array of
  57. *             Gadget pointers.  The Gadgets created by this
  58. *             function can be referenced by the ng_GadgetID of
  59. *             the source NewGadget.
  60. *       TextAttr - (struct TextAttr *) This TextAttr to used for
  61. *             gadgets whose ng_TextAttr is NULL.
  62. *       XScale - the x factor to scale the gadgets.
  63. *       YScale - the y factor to scale the gadgets.
  64. *             
  65. *   RESULT
  66. *       returns a pointer to the context from CreateContext()
  67. *       or NULL on failure.
  68. *
  69. *   EXAMPLE
  70. *
  71. *   NOTES
  72. *       requires diskfont, gadtools & utility libraries to
  73. *       be open.
  74. *
  75. *       This function has a special flag for NewGadget->ng_Flags,
  76. *       NG_FITLABEL.  This flag tells the function to adjust the
  77. *       ng_LeftEdge and/or Width if the PLACETEXT_LEFT or RIGHT
  78. *       flags are set, and the ng_TopEdge and/or Height if the
  79. *       PLACETEXT_ABOVE or BELOW flags are set, so that the 
  80. *       ng_GadgetText will fit inside the area specified for the
  81. *       gadget.  You must also specify one of the PLACETEXT_?
  82. *       values or this will not work.
  83. *
  84. *       For GimmeZeroZero windows set Scr to NULL.
  85. *       
  86. *       This function will not modify any of the arrays 
  87. *       (ie. NewGads, NewGadTags), this way if you have
  88. *       an interface that opens & closes multiple times
  89. *       (like commodities) you will not have to reinitialize
  90. *       the arrays.
  91. *
  92. *       Every ng_GadgetID should be unique and none may have a
  93. *       larger value than the number of elements in the array
  94. *       pointed to by Gadgets.
  95. *
  96. *       GTLV_ShowSelected works differently with this function.
  97. *       specify -1l for the display only gadget, specify the 
  98. *       gadget _id_ of the string gadget for the editable gadget
  99. *       (the string gadget must be created before the listview).
  100. *       Gadtools wants a NULL or poiner to a gadget for this value
  101. *       but this is taken care by this function. 
  102. *
  103. *       Gadgets created by this function can be freed with a 
  104. *       call to FreeGadgets().
  105. *
  106. *   BUGS
  107. *
  108. *   SEE ALSO
  109. *       GetGUIScale()
  110. ******************************************************************************
  111. *
  112. */
  113.  
  114. struct Gadget *MakeGadgets(struct Screen    *Scr,
  115.                            APTR             VisualInfo,
  116.                            ULONG            NumGadgets,
  117.                            struct NewGadget *NewGads,
  118.                            ULONG            *NewGadTags,
  119.                            ULONG            *NewGadKinds,
  120.                            struct Gadget    **Gadgets,
  121.                            struct TextAttr  *TA,
  122.                            float            XMult,
  123.                            float            YMult)
  124. {
  125.   UBYTE underscore[2];
  126.   ULONG  l,tgn;
  127.   struct TextFont *tf;
  128.   struct NewGadget ng;
  129.   struct Gadget *check,*gad,*gfirst=NULL;
  130.   struct TagItem *ti,*ts,*worktags;
  131.   WORD   wx,wy,labelwidth,labelheight;
  132. //  WORD   totwidth,totheight;
  133.  
  134.   if(Scr)
  135.   {
  136.     wx=Scr->WBorLeft;
  137.     wy=Scr->WBorTop + Scr->RastPort.TxHeight + 1;
  138.   }
  139.   else
  140.     wx=wy=0;
  141.     
  142.              
  143.   if(tf=OpenDiskFont(TA))
  144.   {
  145.     if(gad=CreateContext(&gfirst))
  146.     {    
  147.       ng.ng_TextAttr   = TA;
  148.       ng.ng_VisualInfo = VisualInfo;
  149.       tgn=0;
  150.       for(l=0;l<NumGadgets;l++)
  151.       {
  152.         ng.ng_LeftEdge  =NewGads[l].ng_LeftEdge * XMult + wx;
  153.         ng.ng_TopEdge   =NewGads[l].ng_TopEdge  * YMult + wy;
  154.         ng.ng_Width     =NewGads[l].ng_Width    * XMult;
  155.         ng.ng_Height    =NewGads[l].ng_Height   * YMult;
  156.         ng.ng_GadgetText=NewGads[l].ng_GadgetText;
  157.         ng.ng_GadgetID  =NewGads[l].ng_GadgetID;
  158.         ng.ng_Flags     =NewGads[l].ng_Flags & 0x7fffffff;
  159.         ng.ng_UserData  =NewGads[l].ng_UserData;
  160.         if(NewGads[l].ng_TextAttr)
  161.           ng.ng_TextAttr=NewGads[l].ng_TextAttr;
  162.         else
  163.           ng.ng_TextAttr=TA;
  164.           
  165. //        totwidth    =ng.ng_Width;
  166. //        totheight   =ng.ng_Height;
  167.         
  168.         labelwidth=gui_StrFontLen(tf,ng.ng_GadgetText)+8;
  169.         labelheight=tf->tf_YSize;
  170.  
  171.         underscore[0]=GetTagData(GT_Underscore,0,(struct TagItem *)NewGadTags);
  172.         underscore[1]=0;
  173.  
  174.         if(underscore[0] && ng.ng_GadgetText)
  175.           if(strchr(ng.ng_GadgetText,underscore[0]))
  176.             labelwidth-=gui_StrFontLen(tf,underscore);
  177. /*
  178.         // Justtification 
  179.         if(ng.ng_Flags & NG_JUST_RIGHT)
  180.           ng.ng_LeftEdge -=ng.ng_Width + (ng.ng_Flags & NG_JUST_LABEL && ng.ng_Flags & (PLACETEXT_RIGHT|PLACETEXT_IN) ? labelwidth:0) ;
  181.         else
  182.           if(ng.ng_Flags & NG_JUST_HORIZCENTER)
  183.             ng.ng_LeftEdge-=(ng.ng_Width + (ng.ng_Flags & NG_JUST_LABEL && ng.ng_Flags & (PLACETEXT_RIGHT|PLACETEXT_LEFT) ? labelwidth:0))/2;
  184.           else // NG_JUST_LEFT
  185.             ng.ng_LeftEdge +=(ng.ng_Flags & NG_JUST_LABEL && ng.ng_Flags & PLACETEXT_LEFT ? labelwidth:0);
  186. */
  187.         /*
  188.         
  189.         // verticle 
  190.         if(ng.ng_Flags & NG_JUST_BOTTOM)
  191.           ng.ng_TopEdge-=ng.ng_Height+labelheight;
  192.         else
  193.           if(ng.ng_Flags & NG_JUST_VERTCENTER)
  194.             ng.ng_TopEdge-=(ng.ng_Height+labelheight)/2;
  195.           else
  196.             ng.ng_TopEdge+=labelheight;
  197.         */
  198.  
  199.  
  200.         
  201.         if(ng.ng_Flags & NG_FITLABEL)
  202.         {
  203.           if(ng.ng_Flags & PLACETEXT_ABOVE)
  204.           {
  205.             ng.ng_TopEdge+=labelheight;
  206.             ng.ng_Height -=labelheight;
  207.           }
  208.           else
  209.             if(ng.ng_Flags & PLACETEXT_BELOW)
  210.             {
  211.               ng.ng_Height -=labelheight;
  212.             }
  213.             else
  214.             {
  215.  
  216.               if(ng.ng_Flags & PLACETEXT_LEFT)
  217.               {
  218.                 ng.ng_LeftEdge+=labelwidth;
  219.                 ng.ng_Width   -=labelwidth;
  220.               }
  221.               else
  222.                 if(ng.ng_Flags & PLACETEXT_RIGHT)
  223.                 {
  224.                   ng.ng_Width -=labelwidth;
  225.                 }
  226.                 else
  227.                 {
  228.                   if(ng.ng_Flags & PLACETEXT_IN)
  229.                   {
  230.                     ng.ng_Width +=labelwidth;
  231.                     //owidth=ng.ng_Width;
  232.                   }
  233.                 }
  234.             }
  235.         }
  236.         else
  237.  
  238.         ng.ng_Flags&=NG_REAL_GT_FLAGS;
  239.         
  240.         check=0;
  241.   
  242.         if(worktags=CloneTagItems((struct TagItem *)&NewGadTags[tgn]))
  243.         {
  244.           ts=worktags;
  245.   
  246.           switch(NewGadKinds[l])
  247.           {
  248.             //case BUTTON_KIND:      
  249.             //case CHECKBOX_KIND:
  250.             //case CYCLE_KIND:
  251.             //case INTEGER_KIND:
  252.             case LISTVIEW_KIND:
  253.               while(ti=NextTagItem(&ts))
  254.                 switch(ti->ti_Tag)
  255.                 {
  256.                   case GTLV_ShowSelected:
  257.                     if(ti->ti_Data!=~0)
  258.                       ti->ti_Data=(ULONG)Gadgets[ti->ti_Data];
  259.                     else
  260.                       ti->ti_Data=0;
  261.                     break;
  262.                   case GTLV_ScrollWidth:
  263.                     ti->ti_Data*=XMult;
  264.                     break;
  265.                   case GTLV_ItemHeight:
  266.                     ti->ti_Data*=YMult;
  267.                     break;
  268.                 }
  269.               break;
  270.             case MX_KIND:
  271.               while(ti=NextTagItem(&ts))
  272.                 switch(ti->ti_Tag)
  273.                 {
  274.                   case GTMX_Spacing:
  275.                   case LAYOUTA_Spacing:
  276.                     ti->ti_Data*=YMult;
  277.                     break;
  278.                 }
  279.               break;
  280.             case PALETTE_KIND:
  281.               while(ti=NextTagItem(&ts))
  282.                 switch(ti->ti_Tag)
  283.                 {
  284.                   case GTPA_IndicatorWidth:
  285.                     ti->ti_Data*=XMult;
  286.                     break;
  287.                   case GTPA_IndicatorHeight:
  288.                     ti->ti_Data*=YMult;
  289.                     break;
  290.                 }
  291.               break;
  292.             case SCROLLER_KIND:
  293.               if(ti=FindTagItem(GTSC_Arrows ,worktags))
  294.                 switch(GetTagData(PGA_Freedom,LORIENT_HORIZ,worktags))
  295.                 {
  296.                   case LORIENT_HORIZ:
  297.                     ti->ti_Data*=XMult;
  298.                     break;
  299.                   case LORIENT_VERT:
  300.                     ti->ti_Data*=YMult;
  301.                     break;
  302.                 }
  303.               break;
  304.             case SLIDER_KIND:
  305.               if(ti=FindTagItem(GTSL_MaxPixelLen ,worktags))
  306.                 ti->ti_Data*=XMult;
  307.               break;
  308.           }
  309.           check=Gadgets[ng.ng_GadgetID]=gad=CreateGadgetA(NewGadKinds[l],gad,&ng,worktags);
  310.     
  311.           FreeTagItems(worktags);
  312.         }     
  313.         if(!check)
  314.         {
  315.           FreeGadgets(gfirst);
  316.           CloseFont(tf);
  317.           return(NULL);
  318.         }
  319.         while(NewGadTags[tgn]) tgn+=2;
  320.           tgn++;
  321.       }
  322.     }  
  323.     CloseFont(tf);
  324.   }
  325.   return(gfirst);                 
  326. }
  327.  
  328.  
  329.  
  330.  
  331. /*
  332. struct Gadget *MakeSimpleGadgets(struct Screen    *Scr,
  333.                                  struct NewGadget *NewGads,
  334.                                  ULONG            *NewGadTags,
  335.                                  ULONG            *NewGadTypes,
  336.                                  struct Gadget   **Gadgets)
  337. {
  338.   int     l,tgn;
  339.   struct  NewGadget ng;
  340.   struct  Gadget *gad,*gfirst=NULL;
  341.   struct  TagItem *ti;
  342.   ULONG   OldTagValue;
  343.   BOOL    ResetTag;
  344.   WORD    wx,wy;
  345.   
  346.   wx=Scr->WBorLeft;
  347.   wy=Scr->WBorTop + Scr->RastPort.TxHeight + 1;
  348.                
  349.   if(gad=CreateContext(&gfirst))
  350.   {    
  351.     ng.ng_VisualInfo = VisualInfo;
  352.     tgn=0;
  353.     for(l=0;l<GADGETS;l++)
  354.     {
  355.       ng.ng_LeftEdge  =NewGads[l].ng_LeftEdge+wx;
  356.       ng.ng_TopEdge   =NewGads[l].ng_TopEdge+wy;
  357.       ng.ng_Width     =NewGads[l].ng_Width;
  358.       ng.ng_Height    =NewGads[l].ng_Height;
  359.       ng.ng_GadgetText=NewGads[l].ng_GadgetText;
  360.       ng.ng_GadgetID  =NewGads[l].ng_GadgetID;
  361.       ng.ng_Flags     =NewGads[l].ng_Flags;
  362.  
  363.       ResetTag=FALSE;
  364.  
  365.       if(ng.ng_GadgetID==GD_LIST)
  366.       {
  367.         if(ti=FindTagItem(GTLV_ShowSelected,(struct TagItem *)&NewGadTags[tgn]))
  368.         {
  369.           ResetTag=TRUE;
  370.           OldTagValue=ti->ti_Data;
  371.           ti->ti_Data=(ULONG)Gadgets[GD_NAME];
  372.         }
  373.       }
  374.       
  375.       Gadgets[ng.ng_GadgetID] = 
  376.           gad = CreateGadgetA(NewGadTypes[l],gad,&ng,(struct TagItem *)&NewGadTags[tgn]);
  377.       
  378.       if(ResetTag)
  379.         if(ti=FindTagItem(GTLV_ShowSelected,(struct TagItem *)&NewGadTags[tgn]))
  380.           ti->ti_Data=OldTagValue;
  381.       
  382.       if(!gad)
  383.       {
  384.         FreeGadgets(gfirst);
  385.         return(NULL);
  386.       }
  387.       while(NewGadTags[tgn]) tgn+=2;
  388.         tgn++;
  389.     }
  390.   }  
  391.   return(gfirst);                 
  392. }
  393. */
  394.